﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Restaurant.Modules.BIZZ.LocalService;
using System.ComponentModel;
using Sector7.Restaurant.Modules.BIZZ.Utility;
using Sector7.Framework.Enums;
using Sector7.Framework.Utility;
using Sector7.Restaurant.Biz;

namespace Sector7.Restaurant.Modules.BIZZ.Controls
{
    /// <summary>
    /// PurchaseDetailControl.xaml 的交互逻辑
    /// </summary>
    public partial class PurchaseDetailControl : UserControl
    {
        /// <summary>
        /// 销售单信息
        /// </summary>
        private RS_PurchaseBill _purchaseBill = null;

        /// <summary>
        /// 获取单位列表
        /// </summary>
        public List<RS_Unit> Units { get; set; }

        /// <summary>
        /// 获取元数据列表
        /// </summary>
        public List<RS_Metadata> Metadatas { get; set; }

        /// <summary>
        /// 获取或设置采购单信息
        /// </summary>
        public RS_PurchaseBill PurchaseBill
        {
            get { return _purchaseBill; }
        }

        public PurchaseDetailControl()
        {
            InitializeComponent();

            this.DataContext = this;
        }

        public void RemoveInvalidItem()
        {
            for (int i = 0; i < _purchaseBill.Items.Count; )
            {
                if (CheckItemIsValid(_purchaseBill.Items[i]))
                {
                    ++i;
                }
                else
                {
                    _purchaseBill.Items.RemoveAt(i);
                }
            }
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            if (Environments.CurrentAccount != null)
            {
                _purchaseBill = new RS_PurchaseBill();
                if (_purchaseBill.Items == null)
                    _purchaseBill.Items = new List<RS_PurchaseBillItem>();
                _purchaseBill.OccurTime = DateTime.Now;
                _purchaseBill.Status = (int)BillStatusType.Done;
                _purchaseBill.Remarks = string.Empty;
                _purchaseBill.OperatorId = (long)ReflectionUtil.GetPropertyValue(Environments.CurrentAccount, "UniqueId");
                _purchaseBill.CheckerId = (long)ReflectionUtil.GetPropertyValue(Environments.CurrentAccount, "UniqueId");
                
                txbTitle.Text = _purchaseBill.OccurTime.ToString("yyyy年MM月dd日");
                dtgPurchaseBill.ItemsSource = _purchaseBill.Items;
                txbAmount.DataContext = _purchaseBill;
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            LoadData();
        }

        private bool CheckItemIsValid(RS_PurchaseBillItem item)
        {
            return item.Metadata != null &&
                item.Unit != null &&
                item.UnitPrice > 0 &&
                item.Number > 0;
        }

        private void dtgPurchaseBill_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            var editingElement = e.EditingElement;
            var billItem = e.Row.Item as RS_PurchaseBillItem;

            object value = null;
            switch (e.Column.Header.ToString())
            {
                case "名称":
                    value = editingElement.GetValue(ComboBox.SelectedItemProperty);
                    if (value != null)
                    {
                        billItem.Metadata = value as RS_Metadata;
                        billItem.MetadataId = billItem.Metadata.Id;
                    }
                    break;
                case "单位":
                    value = editingElement.GetValue(ComboBox.SelectedItemProperty);
                    if (value != null)
                    {
                        billItem.Unit = value as RS_Unit;
                        billItem.UnitId = billItem.Unit.UnitId;
                    }
                    break;
                case "单价":
                    billItem.UnitPrice = Convert.ToDecimal(editingElement.GetValue(TextBox.TextProperty));
                    break;
                case "数量":
                    billItem.Number = Convert.ToDecimal(editingElement.GetValue(TextBox.TextProperty));
                    break;
                default:
                    break;
            }
        }

        private void dtgPurchaseBill_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        {
            var billItem = e.Row.Item as RS_PurchaseBillItem;

            if (CheckItemIsValid(billItem))
            {
                _purchaseBill.Amount += billItem.UnitPrice * billItem.Number;
            }
            else
            {
                e.Cancel = true;
            }
        }
    }
}
